package com.pricy.me.model;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class TempData {
	public static int USER = 0;
	public static int CURRENCY = 1;
	public static int CATEGORIE = 2;
	public static int PRODUCT = 3;
	private int type;
	private String url;
	private String data;
	
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
	@SuppressWarnings("unchecked")
	public List<TempData> getList(Context c){
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
	    	String jsonOutput = readFile("tempdata.info", c);
	    	Type listType = new TypeToken<List<TempData>>(){}.getType();
	    	if(jsonOutput.length() < 2){
	    		List<TempData> empty = new ArrayList<TempData>();
	    		jsonOutput = gson.toJson(empty);
	    	}
	    	
			return (List<TempData>) gson.fromJson(jsonOutput, listType);
	}

	public void push(Context c) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
    	String jsonOutput = readFile("tempdata.info", c);
    	Type listType = new TypeToken<List<TempData>>(){}.getType();
    	if(jsonOutput.length() < 2){
    		List<TempData> empty = new ArrayList<TempData>();
    		jsonOutput = gson.toJson(empty);
    	}
    	
		@SuppressWarnings("unchecked")
		List<TempData> list = (List<TempData>) gson.fromJson(jsonOutput, listType);
		if(this.type == CURRENCY || this.type == CATEGORIE){
			int i;
			for (i = 0; i < list.size(); i++) {
					if((list.get(i).getType() == CURRENCY || list.get(i).getType() == CATEGORIE) && list.get(i).getType() == this.type) {
						list.get(i).setData(this.data);
						break;
					}
			}
			Log.e("I", ""+i);
			if(i == list.size()) list.add(this);
		}
		else list.add(this);
		
		String jsonInput = gson.toJson(list);
    	writeFile("tempdata.info", jsonInput, c);
	}
	
	
	protected void writeFile(String file, String text, Context c){
		FileOutputStream fos;
		try {
			fos = c.openFileOutput(file, Context.MODE_PRIVATE);
			fos.write(text.getBytes());
        	fos.close();
		} catch (FileNotFoundException e) {
			Toast.makeText(c, "ERROR: can't open file", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(c, "ERROR: can't write file", Toast.LENGTH_SHORT).show();
		}	
	}
    
    protected String readFile(String file, Context c) {
		StringBuilder json = new StringBuilder();
		try {
			FileInputStream fis = c.openFileInput(file);
			BufferedReader r = new BufferedReader(new InputStreamReader(fis));
			String line;
			while ((line = r.readLine()) != null) {
			    json.append(line);
			}
			fis.close();
			return json.toString();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return "0";
		} catch (IOException e) {
			e.printStackTrace();
			return "0";
		}
	}
	public void clear(Context c) {
		writeFile("tempdata.info", "", c);
		
	}
	
}
